package cn.iocoder.yudao.module.peizhen.controller.admin.opencity;


import cn.iocoder.yudao.module.peizhen.dal.dataobject.opencity.ProgramDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.peizhen.controller.admin.opencity.vo.*;
import cn.iocoder.yudao.module.peizhen.dal.dataobject.opencity.OpenCityDO;
import cn.iocoder.yudao.module.peizhen.service.opencity.OpenCityService;

@Tag(name = "管理后台 - 服务开通城市")
@RestController
@RequestMapping("/peizhen/open-city")
@Validated
public class OpenCityController {

    @Resource
    private OpenCityService openCityService;

    @PostMapping("/create")
    @Operation(summary = "创建服务开通城市")
    @PreAuthorize("@ss.hasPermission('peizhen:open-city:create')")
    public CommonResult<Integer> createOpenCity(@Valid @RequestBody OpenCitySaveReqVO createReqVO) {
        return success(openCityService.createOpenCity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新服务开通城市")
    @PreAuthorize("@ss.hasPermission('peizhen:open-city:update')")
    public CommonResult<Boolean> updateOpenCity(@Valid @RequestBody OpenCitySaveReqVO updateReqVO) {
        openCityService.updateOpenCity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除服务开通城市")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('peizhen:open-city:delete')")
    public CommonResult<Boolean> deleteOpenCity(@RequestParam("id") Integer id) {
        openCityService.deleteOpenCity(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得服务开通城市")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('peizhen:open-city:query')")
    public CommonResult<OpenCityRespVO> getOpenCity(@RequestParam("id") Integer id) {
        OpenCityDO openCity = openCityService.getOpenCity(id);
        return success(BeanUtils.toBean(openCity, OpenCityRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得服务开通城市分页")
    @PreAuthorize("@ss.hasPermission('peizhen:open-city:query')")
    public CommonResult<PageResult<OpenCityRespVO>> getOpenCityPage(@Valid OpenCityPageReqVO pageReqVO) {
        PageResult<OpenCityDO> pageResult = openCityService.getOpenCityPage(pageReqVO);
        PageResult<OpenCityRespVO> openCityRespVOPageResult = BeanUtils.toBean(pageResult, OpenCityRespVO.class);
        List<OpenCityRespVO> list = openCityRespVOPageResult.getList();
        for (OpenCityRespVO openCityRespVO : list) {
            OpenCityDO province = openCityService.getOpenCity(openCityRespVO.getParentId());
            openCityRespVO.setParentName(province.getName());
        }
        return success(openCityRespVOPageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出服务开通城市 Excel")
    @PreAuthorize("@ss.hasPermission('peizhen:open-city:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOpenCityExcel(@Valid OpenCityPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OpenCityDO> list = openCityService.getOpenCityPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "服务开通城市.xls", "数据", OpenCityRespVO.class,
                        BeanUtils.toBean(list, OpenCityRespVO.class));
    }

    @GetMapping("/getAllCity")
    @Operation(summary = "获取省市信息")
    @PermitAll
    public CommonResult<List<AreaNodeRespVO>> getAllCity() {
        List<OpenCityDO> openCityDOS = openCityService.getAllCity();
        List<AreaNodeRespVO> openCityRespVOS = BeanUtils.toBean(openCityDOS, AreaNodeRespVO.class);
        //获取省
        List<AreaNodeRespVO> pronvince=openCityRespVOS.stream().filter(e -> e.getType() ==2)
                .collect(Collectors.toList());
        //给省装市
        for (AreaNodeRespVO areaNodeRespVO : pronvince) {
            List<AreaNodeRespVO> collect = openCityRespVOS.stream().filter(e -> e.getParentId().equals(areaNodeRespVO.getId()) ).collect(Collectors.toList());
            areaNodeRespVO.setChildren(collect);
        }
        return success(pronvince);
    }

    // ==================== 子表（具体服务） ====================

    @GetMapping("/program/page")
    @Operation(summary = "获得具体服务分页")
    @Parameter(name = "cityCode", description = "市编码")
    @PreAuthorize("@ss.hasPermission('peizhen:open-city:query')")
    public CommonResult<PageResult<ProgramDO>> getProgramPage(PageParam pageReqVO,
                                                              @RequestParam("cityCode") Integer cityCode) {
        return success(openCityService.getProgramPage(pageReqVO, cityCode));
    }

    @PostMapping("/program/create")
    @Operation(summary = "创建具体服务")
    @PreAuthorize("@ss.hasPermission('peizhen:open-city:create')")
    public CommonResult<Integer> createProgram(@Valid @RequestBody ProgramDO program) {
        return success(openCityService.createProgram(program));
    }

    @PutMapping("/program/update")
    @Operation(summary = "更新具体服务")
    @PreAuthorize("@ss.hasPermission('peizhen:open-city:update')")
    public CommonResult<Boolean> updateProgram(@Valid @RequestBody ProgramDO program) {
        openCityService.updateProgram(program);
        return success(true);
    }

    @DeleteMapping("/program/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除具体服务")
    @PreAuthorize("@ss.hasPermission('peizhen:open-city:delete')")
    public CommonResult<Boolean> deleteProgram(@RequestParam("id") Integer id) {
        openCityService.deleteProgram(id);
        return success(true);
    }

    @GetMapping("/program/get")
    @Operation(summary = "获得具体服务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('peizhen:open-city:query')")
    public CommonResult<ProgramDO> getProgram(@RequestParam("id") Integer id) {
        return success(openCityService.getProgram(id));
    }
}